package com.example.testproj.c0403;

import android.animation.IntEvaluator;
import android.animation.ObjectAnimator;
import android.animation.TypeEvaluator;
import android.animation.ValueAnimator;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.BounceInterpolator;

import com.example.testproj.R;

public class AnimateTestActivity extends AppCompatActivity implements View.OnClickListener {
    private static final String TAG = "AnimateTestActivity";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_animate_test);
        findViewById(R.id.button2).setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.button2) {
           // performAnimate();
            performAnimate(findViewById(R.id.button2),findViewById(R.id.button2).getWidth(),500);
        }
    }

    private void performAnimate(final View target,final int start,final int end){
        ValueAnimator valueAnimator=ValueAnimator.ofInt(1,100);

        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            private MyTypeEvaluator mEvaluator=new MyTypeEvaluator();
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                int currentValue= (int) animation.getAnimatedValue();

                float fraction=currentValue/100f;

                target.getLayoutParams().width=mEvaluator.evaluate(fraction,start,end);
                target.requestLayout();
            }
        });
        valueAnimator.setInterpolator(new BounceInterpolator());
        valueAnimator.setDuration(5000).start();
    }

    private void performAnimate() {
        ObjectAnimator animator=  ObjectAnimator.ofInt(new ViewWrapper(findViewById(R.id.button2)),"width",500).setDuration(5000);
        animator.setEvaluator(new MyTypeEvaluator());
        animator.setInterpolator(new BounceInterpolator());
        animator.start();
    }

    private static class ViewWrapper{
        private View mTarget;

        public ViewWrapper(View mTarget) {
            this.mTarget = mTarget;
        }

        public int getWidth(){
            Log.d(TAG, "getWidth: "+mTarget.getLayoutParams().width);
            return mTarget.getMeasuredWidth();
        }

        public void setWidth(int width){
            Log.d(TAG, "setWidth: "+width);
            mTarget.getLayoutParams().width=width;
            mTarget.requestLayout();
        }
    }

    private static class MyTypeEvaluator implements TypeEvaluator<Integer>{

        @Override
        public Integer evaluate(float fraction, Integer startValue, Integer endValue) {
            int value= (int) (fraction*(endValue-startValue)+startValue);
            Log.d(TAG, "evaluate:value== "+value);
            return value;
        }
    }
}
